<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <button id="btn">获取歌手信息</button>
  <div id="app"></div>
  <ul id="list">

  </ul>

  <script>
    let btn = document.getElementById('btn')
    let rowData=[]
    btn.addEventListener('click', () => {
      // 获取歌手数据
      
      
        getData().then(()=>{
      randerList(rowData)
   })
    })

    function getData() {//网络请求是耗时的异步
     return new Promise((reslove,reject)=>{
          
             const xhr = new XMLHttpRequest()  // 创建异步对象
      xhr.open('GET', 'https://mock.mengxuegu.com/mock/66585c4db462b81cb3916d3e/songer/songer?', true) // 准备请求的参数
      xhr.send() // 发送请求
      // 监测请求的发送结果
      xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200) { // 请求成功
            let data =JSON.parse(xhr.responseText)//把json字符串转换成json对象
          console.log(data.data);    
          //randerList(data.data)
          rowData=data.data
           reslove()
        
        }
      }
           
    

     })
     
    }
   function randerList(arr){
    let list =document.getElementById('list')
    for(let i=0;i<arr.length;i++)
   {
    let item =arr[i]
    let li =document.createElement('li')
    li.innerText=`${item.name}--${item.songsCount}`
    list.appendChild(li)
   }
   }
 
  
  
  </script>
</body>
</html>
